﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KBManagement.DAL;
using KBManagement.BIZ;
using KBManagement.Library;

namespace KBManagement
{
    public partial class ExtraFees : Form,InforObserver
    {
        KBSQLClassDataContext context;
        public ExtraFees()
        {
            context = Utils.CreateInstance().GetDBContext();
            Utils.CreateInstance().AddInforObserver(this);
            InitializeComponent();
            selectCategoryExtrafree();
            selectExtrafree();

        }

        public void selectCategoryExtrafree()
        {
            var categoryExtrafree = from fc in context.CategoryExtraFees select new { 
            fc.CEF_ID,
            fc.CEF_Name
            };
            dataGridView1.DataSource = categoryExtrafree;
            dataGridView1.Columns[0].HeaderText = "Mã số";
            dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns[1].HeaderText = "Tên loại phụ phí";
        }

        public void selectExtrafree()
        {
            var extrafree = from fc in context.ExtraFees select new { 
                fc.EF_ID,
                fc.EF_Name,
                fc.EF_MONEY,
                fc.CategoryExtraFee.CEF_Name
            };
            dataGridView2.DataSource = extrafree;
            dataGridView2.Columns[0].HeaderText = "Mã số";
            dataGridView2.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView2.Columns[1].HeaderText = "Tên phụ phí";
            dataGridView2.Columns[2].HeaderText = "Số tiền";
            dataGridView2.Columns[3].HeaderText = "Tên loại phụ phí";
        }


        public void selectExtrafreeByCaId(string caId)
        {
            var extrafree = from fc in context.ExtraFees
                            where fc.CEF_ID == Convert.ToInt32(caId)
                            select new
                            {
                                fc.EF_ID,
                                fc.EF_Name,
                                fc.EF_MONEY,
                                //fc.MonthSalary.PayMonth,
                                //fc.MonthSalary.PayYear,
                                fc.CategoryExtraFee.CEF_Name
                            };
            dataGridView2.DataSource = extrafree;
            dataGridView2.Columns[0].HeaderText = "Mã số";
            dataGridView2.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView2.Columns[1].HeaderText = "Tên phụ phí";
            dataGridView2.Columns[2].HeaderText = "Số tiền";
            //dataGridView2.Columns[3].HeaderText = "Tháng";
            //dataGridView2.Columns[4].HeaderText = "Năm";
            dataGridView2.Columns[5].HeaderText = "Tên loại phụ phí";
        }

        private void toolStripBtnAddCateService_Click(object sender, EventArgs e)
        {
            new AddCategoryEF().ShowDialog();
        }

        public void Update()
        {
            selectCategoryExtrafree();
            selectExtrafree();
        }

        private void toolStripBtnAddService_Click(object sender, EventArgs e)
        {
            //DateTime dateNow = DateTime.Now;
            //int month = dateNow.Month;
            //int year = dateNow.Year;
            //MonthSalary monthSalary = context.MonthSalaries.SingleOrDefault(x => x.PayMonth == month && x.PayYear == year);
            //if (monthSalary == null)
            //{
            //    insertMonthSalary(month,year);
            //}
            //else{
            //    monthSalary.Date = dateNow;        
            //}
            //context.SubmitChanges();
            new AddEditExtraFees().ShowDialog();
        }

        public void insertMonthSalary(int month,int year)
        {
            //MonthSalary monthSalary = new MonthSalary { 
            //Date = DateTime.Now,
            //PayMonth = month,
            //PayYear = year
            //};
            //context.MonthSalaries.InsertOnSubmit(monthSalary);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    selectExtrafreeByCaId(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                }
            }
            catch (Exception)
            {
                
                
            }
        }
    }
}
